﻿#ifndef MODBUSTCPSERVER_H
#define MODBUSTCPSERVER_H

#include <QObject>
#include <QModbusTcpServer>
#include <QTcpSocket>
#include <QHostAddress>
#include "PLCServerBase.h"

class ModbusTcpServer : public PLCServerBase
{
    Q_OBJECT
public:
    explicit ModbusTcpServer(QObject *parent = nullptr);
    virtual void initCom() override;
    virtual void onClientConnect(QString addr) override;
private:
    class ModbusTcpConnectionObserver* observer = nullptr;
};

class ModbusTcpConnectionObserver:public QModbusTcpConnectionObserver{

    // QModbusTcpConnectionObserver interface
public:
    bool acceptNewConnection(QTcpSocket *newClient){
        server->onClientConnect(newClient->localAddress().toString());
        return true;
    }
    void setServer(ModbusTcpServer* plc){server = plc;}

    ModbusTcpServer* server;
};
#endif // MODBUSTCPSERVER_H
